﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using AdoNet = System.Data;

namespace DataSet4Com
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComDefaultInterface(typeof(IDataColumn))]
    [Guid("7FB27A8B-0029-4F4F-AEB1-902A9799C3E5")]
    public class DataColumn : IDataColumn
    {
        private AdoNet.DataColumn _adoNetColumn;

        public DataColumn() : this(null)
        {
        }

        public DataColumn(AdoNet.DataColumn adoNetColumn)
        {
            if (adoNetColumn == null)            
                _adoNetColumn = new AdoNet.DataColumn();
            else
                _adoNetColumn = adoNetColumn;
        }

        [ComVisible(false)]
        public AdoNet.DataColumn AdoNetDataColumn
        {
            get { return _adoNetColumn; }
        }

        public bool AllowDBNull
        {
            get { return _adoNetColumn.AllowDBNull; }
            set { _adoNetColumn.AllowDBNull = value; }
        }

        public bool AutoIncrement
        {
            get { return _adoNetColumn.AutoIncrement; }
            set { _adoNetColumn.AutoIncrement = value; }
        }

        public long AutoIncrementSeed
        {
            get { return _adoNetColumn.AutoIncrementSeed; }
            set { _adoNetColumn.AutoIncrementSeed = value; }
        }

        public long AutoIncrementStep
        {
            get { return _adoNetColumn.AutoIncrementStep; }
            set { _adoNetColumn.AutoIncrementStep = value; }
        }

        public string Caption
        {
            get { return _adoNetColumn.Caption; }
            set { _adoNetColumn.Caption = value; }
        }

        public virtual MappingType ColumnMapping
        {
            get { return (MappingType)((int)_adoNetColumn.ColumnMapping); }
            set { _adoNetColumn.ColumnMapping = (AdoNet.MappingType)((int)value); }
        }

        public string ColumnName
        {
            get { return _adoNetColumn.ColumnName; }
            set { _adoNetColumn.ColumnName = value; }
        }

        public string DataType
        {
            get { return _adoNetColumn.DataType.Name; }
            set { _adoNetColumn.DataType = Type.GetType(value); }
        }

        public DataSetDateTime DateTimeMode
        {
            get { return (DataSetDateTime)((int)_adoNetColumn.DateTimeMode); }
            set { _adoNetColumn.DateTimeMode = (AdoNet.DataSetDateTime)((int)value); }
        }

        public object DefaultValue
        {
            get { return _adoNetColumn.DefaultValue; }
            set { _adoNetColumn.DefaultValue = value; }
        }

        public string Expression
        {
            get { return _adoNetColumn.Expression; }
            set { _adoNetColumn.Expression = value; }
        }

        public int MaxLength
        {
            get { return _adoNetColumn.MaxLength; }
            set { _adoNetColumn.MaxLength = value; }
        }

        public string Namespace
        {
            get { return _adoNetColumn.Namespace; }
            set { _adoNetColumn.Namespace = value; }
        }

        public int Ordinal
        {
            get { return _adoNetColumn.Ordinal; }         
        }

        public string Prefix
        {
            get { return _adoNetColumn.Prefix; }
            set { _adoNetColumn.Prefix = value; }
        }

        public bool ReadOnly
        {
            get { return _adoNetColumn.ReadOnly; }
            set { _adoNetColumn.ReadOnly = value; }
        }

        public DataTable Table
        {
            get { return new DataTable(_adoNetColumn.Table); }         
        }

        public bool Unique
        {
            get { return _adoNetColumn.Unique; }
            set { _adoNetColumn.Unique = value; }
        }

        public void SetOrdinal(int ordinal)
        {
            _adoNetColumn.SetOrdinal(ordinal);
        }

        public override string ToString()
        {
            return _adoNetColumn.ToString();
        }
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("88D3BE8B-5FBC-4CF8-B616-39515C36C250")]
    public interface IDataColumn
    {
        bool AllowDBNull { get; set; }

        bool AutoIncrement { get; set; }

        long AutoIncrementSeed { get; set; }

        long AutoIncrementStep { get; set; }

        string Caption { get; set; }

        MappingType ColumnMapping { get; set; }

        string ColumnName { get; set; }

        string DataType { get; set; }

        DataSetDateTime DateTimeMode { get; set; }

        object DefaultValue { get; set; }

        string Expression { get; set; }

        int MaxLength { get; set; }

        string Namespace { get; set; }

        int Ordinal { get; }

        string Prefix { get; set; }

        bool ReadOnly { get; set; }

        DataTable Table { get; }

        bool Unique { get; set; }

        void SetOrdinal(int ordinal);
                
        string ToString();
    }
}
